<?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

$db = new Mypdo();

if(empty($_GET['id'])){
$locaManager = new LocataireManager($db);
$loca = $locaManager->getList();

?>
<h1>Choix du locataires</h1>
<table class="tableau" >
    <tr>
        <th>Nom</th>
        <th>Prenom</th>
        <th>Location</th>
        <th>Appel</th>
    </tr>
    <?php foreach ($loca as $valeur) {
        $loc = $locaManager->getLocation($valeur->getIdLoca());
        $pers = $locaManager->getPersonne($valeur->getIdLoca());

//var_dump($pers);
        ?>

        <tr>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getNomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getNomPers().'<br/>';
                    }
                }

                ?></td>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getPrenomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getPrenomPers().'<br/>';
                    }
                }?></td>
            <td><?php
                if(gettype($loc) == 'object'){
                    echo $loc->getNomLoc();
                }else{
                    foreach($loc as $val){
                        echo $val->getNomLoc().'<br/>';
                    }
                }
                ?></td>
            <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=210&id=<?php echo $valeur->getIdLoca(); ?>'" /></td>
        </tr>

    <?php
    } ?>
</table>

<?php
}elseif(empty($_POST)&&!empty($_GET['id'])){

$locaManager = new LocataireManager($db);
$liste = $locaManager->getLocation($_GET['id']);
$pers = $locaManager->getPersonne($_GET['id']);
//var_dump($pers);
    ?>
    <h1>Nouvel Appel de Loyer</h1>
    <form method="post" action="#">

        <label for="id_loc">Location :</label><select name="id_loc" id="id_loc" required="required">
            <?php
            if(gettype($liste) == 'object'){
                ?><option  value="<?php echo $liste->getIdLoc(); ?>"><?php echo $liste->getNomLoc(); ?></option><?php echo "\n";
            }else{
                ?><option></option><?php
                foreach($liste as $key=>$value){
                    ?><option value="<?php echo $value->getIdLoc(); ?>"><?php echo $value->getNomLoc(); ?></option><?php echo "\n";
                }
            }?>


        </select>

        <label for="date_loyer">Date de l'appel</label>
        <input type="date" id="date_loyer" name="date_loyer"  required="required" />
        <label for="imp">Imprimer :</label>
        <input type="checkbox" id="imp" name="imp" class="change" checked <?php /* onclick="GereControle('imp','id_pers',1);GereControle('imp','l_pers',1);" */ ?> />
        <label id="l_pers" for="id_pers" class="changer"  >Personne :</label>
        <select class="changer" id="id_pers" name="id_pers" required="" >

            <?php
            if(gettype($pers) == 'object'){
                ?><option selected value="<?php echo $pers->getIdPers(); ?>"><?php echo $pers->getNomPers().' '.$pers->getPrenomPers() ?></option><?php echo "\n";

            }else{
                ?><option></option><?php
                foreach ($pers as $value) {
                    //var_dump($value);
                    ?>
                <option value="<?php echo $value->getIdPers(); ?>"><?php echo $value->getNomPers().' '.$value->getPrenomPers() ?></option><?php echo "\n"; ?>
                <?php
                }

            }
            ?>
        </select>
        <p><input type="reset" value="Annuler" /><input type="submit" value="Valider" /></p>


    </form>


    <?php

}elseif(!empty($_POST)&&!empty($_GET['id'])){
    try{
        $loyerManager = new LoyerManager($db);
        $loyer = new Loyer(array('id_loc'=>$_POST['id_loc'],'id_loca'=>$_GET['id'],'date_loyer'=>$_POST['date_loyer'],'statut_loyer'=>0));
        LoyerRegle::verifAppel($loyer,$db);
        $loyerManager->add($loyer);
        if($_POST['imp'] != 'on'){
            header('Location: index.php?page=201');
        }else{
            $_SESSION['id_loca'] = $_GET['id'];
            $_SESSION['id_loc'] = $_POST['id_loc'];
            $_SESSION['date_loyer'] = $_POST['date_loyer'];
            $_SESSION['id_pers'] = $_POST['id_pers'];
            $_SESSION['ligne_max'] = '0';


            header('Location: include/pages/impAppel.php');
        }
    }catch (ImmolocException $e){
        ?>

        <div class="erreurSaisi"><p>
                <?php
                echo $e->getMessage();
                //header('Location: #');

                ?>
            </p>
        </div>

    <?php
    }

}
}else{
    header('Location: index.php');
}